﻿using System;
using System.IO;
using System.Text;

namespace MouseExtender.Logic.Helpers
{
    public class PathHelper
    {
        private static string RelativeToPath(string absolutePath, string relativeTo)
        {
            if (!Path.IsPathRooted(relativeTo))
                throw new ArgumentException("relativeTo path should be not relative path");

            if (string.IsNullOrEmpty(relativeTo))
                return absolutePath;

            if (string.IsNullOrEmpty(absolutePath))
                return relativeTo;

            if (!Path.IsPathRooted(absolutePath))
                return absolutePath;

            string rootAbsolute = Path.GetPathRoot(absolutePath);
            string rootRelativeTo = Path.GetPathRoot(relativeTo);

            if (!string.Equals(rootAbsolute, rootRelativeTo, StringComparison.CurrentCultureIgnoreCase))
                return absolutePath;

            //should contains only folders
            string absolutePathFileName = Path.GetFileName(absolutePath);
            absolutePath = Path.GetDirectoryName(absolutePath);
            if (absolutePath == null)
                absolutePath = rootAbsolute;

            string[] absoluteDirectories = absolutePath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            string[] relativeDirectories = relativeTo.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);

            //Get the shortest of the two paths
            int shortestLength = Math.Min(absoluteDirectories.Length, relativeDirectories.Length);

            //Use to determine where in the loop we exited
            int commonRootIndex;

            //Find common root index
            for (commonRootIndex = 0; commonRootIndex < shortestLength; commonRootIndex++)
            {
                if (!string.Equals(absoluteDirectories[commonRootIndex], relativeDirectories[commonRootIndex], StringComparison.CurrentCultureIgnoreCase))
                {
                    break;
                }
            }
            commonRootIndex--;

            StringBuilder relativePathBuilder = new StringBuilder();
            //down to common root with via '..\'
            for (int currentIndex = relativeDirectories.Length - 1; currentIndex > commonRootIndex; currentIndex--)
            {
                relativePathBuilder.Append("..\\");
            }

            //up to absolute path start from common root
            for (int currentIndex = commonRootIndex + 1; currentIndex < absoluteDirectories.Length; currentIndex++)
            {
                relativePathBuilder.Append(absoluteDirectories[currentIndex] + "\\");
            }

            string relativePath = Path.Combine(relativePathBuilder.ToString(), absolutePathFileName);
            return relativePath;
        }

        public static string RelativeToAppBaseFolderPath(string absolutePath)
        {
            return RelativeToPath(absolutePath, AppDomain.CurrentDomain.BaseDirectory);
        }

        public static string GetFullPath(string path)
        {
            if (!string.IsNullOrEmpty(path) && !Path.IsPathRooted(path))
            {
                if (Environment.CurrentDirectory != AppDomain.CurrentDomain.BaseDirectory)
                {
                    Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
                }

                path = Path.GetFullPath(path);
            }

            return path;
        }
    }
}
